package com.szq.ai.healthcare.config;

import com.szq.ai.healthcare.store.MongoChatMemoryStore;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: szq
 * @description:
 * @date: 2025-05-28 09:05:52
 * @return
 */
@Configuration
public class AIAgentConfig {

    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Autowired
    private EmbeddingStore embeddingStore;

    @Autowired
    private EmbeddingModel  embeddingModel;

    @Bean
    ChatMemoryProvider chatMemoryProviderAi() {
        return memoryId-> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(20)
                .chatMemoryStore(mongoChatMemoryStore)
                .build();
    }

    /*@Bean
    ContentRetriever contentRetrieverAi() {
        //使用FileSystemDocumentLoader读取指定目录下的知识库文档
        //并使用默认的文档解析器对文档进行解析
        Document document1 = FileSystemDocumentLoader.loadDocument("E:/knowledge/医院信息.md");
        Document document2 = FileSystemDocumentLoader.loadDocument("E:/knowledge/科室信息.md");
        Document document3 = FileSystemDocumentLoader.loadDocument("E:/knowledge/神经内科.md");

        List<Document> list = Arrays.asList(document1, document2, document3);
        //使用内存向量存储
        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
        //使用默认的文档分割器
        EmbeddingStoreIngestor.ingest(list, embeddingStore);
        //从嵌入存储里检索和查询内容相关的信息
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }*/

    @Bean
    ContentRetriever contentRetrieverAi() {
        return EmbeddingStoreContentRetriever
                .builder()
                //设置用于生成嵌入向量的嵌入模型
                .embeddingModel(embeddingModel)
                //指定要使用的嵌入存储
                .embeddingStore(embeddingStore)
                //设置最大检索结果数量，这里表示最多返回1条数据
                .maxResults(1)
                //设置最小得分阈值，只有得分大于等于0.8的结果才会返回
                .minScore(0.8)
                .build();
    }
}
